package com.blkj.spring_boot_blkj.controller;

import com.alibaba.fastjson.JSON;
import com.blkj.spring_boot_blkj.service.WorkFlowService;
import com.blkj.spring_boot_blkj.vo.WorkFlowDataJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.blkj.spring_boot_blkj.pojo.WorkFlowData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
public class WorkFlowController {
    @Autowired
    public WorkFlowService workFlowService;

    @PostMapping("/workflow/workflowdata")
    public String getWorkFlowData(@RequestParam("web_json") String message ,
                                  @RequestParam("id") int messageid) {
        WorkFlowData workFlowData = JSON.parseObject(message, WorkFlowData.class);
        Integer jsonId = messageid;
        workFlowData.setJsonId(jsonId);
        //System.out.println(workFlowData);
        try {
            Integer res = workFlowService.addWorkFlowData(workFlowData);
            if (res == 1) {
                return "success";
            }
            return "failed";
        } catch (Exception exception) {
            return "failed";
        }
     }

    @GetMapping("/get_algorithm_list")
    @ResponseBody
    public List<Map> workFlowData() {
        List<WorkFlowDataJson> WorkFlowDataJsonList= workFlowService.getJsonWorkFlow();
        List<Map> listMap = new ArrayList<>();
        for(WorkFlowDataJson workFlowDataJson:WorkFlowDataJsonList){
            Map map = new HashMap();
            map.put("web_json", workFlowDataJson);
            map.put("id", workFlowDataJson.getJsonId());
            listMap.add(map);
        }
        return listMap;
    }
}
